package com.csii.activiti.server.core.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.csii.activiti.server.abstraction.service.ActBasicService;
import com.csii.activiti.server.core.constant.CommonConstant;
import com.csii.activiti.server.core.entity.db.ActFlowConfigurationEntity;
import com.csii.activiti.server.core.entity.db.ActFlowNodeKeyEntity;
import com.csii.activiti.server.core.exception.RequestParamException;
import com.csii.activiti.server.core.mapper.ActFlowConfigurationMapper;
import com.csii.activiti.server.core.mapper.ActFlowNodeKeyMapper;
import com.csii.activiti.server.core.service.FlowNodeKeyService;
import org.activiti.engine.TaskService;
import org.activiti.engine.task.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class FlowNodeKeyServiceImpl extends ServiceImpl<ActFlowNodeKeyMapper, ActFlowNodeKeyEntity> implements FlowNodeKeyService {

    @Autowired
    private TaskService taskService;

    @Autowired
    private ActFlowConfigurationMapper actFlowConfigurationMapper;
    @Autowired
    private ActBasicService actBasicService;

    private String messageFieldName = "messageKey";

    @Override
    public List<ActFlowNodeKeyEntity> queryFlowNodeKeyList() {
        String currentTenantId = actBasicService.getCurrentTenantId();
        return getBaseMapper().selectList(new LambdaQueryWrapper<ActFlowNodeKeyEntity>().eq(ActFlowNodeKeyEntity::getActTenantId,currentTenantId));
    }

    @Override
    public void editActFlowNodeKey(ActFlowNodeKeyEntity entity) {
        String currentTenantId = actBasicService.getCurrentTenantId();
        Integer count = getBaseMapper().selectCount(new LambdaQueryWrapper<ActFlowNodeKeyEntity>().eq(ActFlowNodeKeyEntity::getNodeKey, entity.getNodeKey()).eq(ActFlowNodeKeyEntity::getActTenantId,currentTenantId).ne(ActFlowNodeKeyEntity::getId,entity.getId()));
        if (count >0){
            throw new RequestParamException("秘钥重复");
        }
        getBaseMapper().updateById(entity);
    }

    @Override
    public void addActFlowNodeKey(ActFlowNodeKeyEntity entity) {
        String currentTenantId = actBasicService.getCurrentTenantId();
        Integer count = getBaseMapper().selectCount(new LambdaQueryWrapper<ActFlowNodeKeyEntity>().eq(ActFlowNodeKeyEntity::getNodeKey, entity.getNodeKey()).eq(ActFlowNodeKeyEntity::getActTenantId,currentTenantId));
        if (count >0){
            throw new RequestParamException("秘钥重复");
        }
        entity.setCreateTime(new Date());
        entity.setActTenantId(currentTenantId);
        getBaseMapper().insert(entity);
    }

    @Override
    public void deleteActFlowNodeKey(String id) {
        String currentTenantId = actBasicService.getCurrentTenantId();
        ActFlowNodeKeyEntity actFlowNodeKeyEntity = getBaseMapper().selectById(id);
        if (actFlowNodeKeyEntity == null){
            throw new RequestParamException("消息秘钥不存在");
        }

        List<Task> list = taskService.createTaskQuery().taskTenantId(currentTenantId).taskAssignee(CommonConstant.MESSAGE_PREFIX_KEY + actFlowNodeKeyEntity.getNodeKey()).list();

        if (CollUtil.isNotEmpty(list)){
            throw new RequestParamException("有进行中流程使用了当前消息秘钥,无法删除");
        }

        Integer count = actFlowConfigurationMapper.selectCount(new LambdaQueryWrapper<ActFlowConfigurationEntity>().eq(ActFlowConfigurationEntity::getActTenantId,currentTenantId).like(ActFlowConfigurationEntity::getJsonData, "\""+messageFieldName+"\":\""+actFlowNodeKeyEntity.getNodeKey()+"\""));

        if (count > 0){
            throw new RequestParamException("有流程设计中依赖当前消息秘钥,无法删除");
        }
        getBaseMapper().deleteById(id);
    }
}
